<template>
  <div>
    <svg-icon @click="openModel" icon-class="password" size="1.2rem" />
    <div>
      <el-dialog width="500px" title="修改密码" :visible.sync="dialogVisible">
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          label-width="120px"
          :rules="rules"
        >
          <el-form-item prop="oldPassword" label="旧密码">
            <el-input
              style="width: 250px"
              v-model="ruleForm.oldPassword"
              autocomplete="off"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="newPassword" label="新密码">
            <el-input
              style="width: 250px"
              v-model="ruleForm.newPassword"
              autocomplete="off"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="checkPassword" label="确认密码">
            <el-input
              style="width: 250px"
              v-model="ruleForm.checkPassword"
              autocomplete="off"
              type="password"
            ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确定</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { updateAdminPassword } from "@/api/user";
export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        oldPassword: "",
        newPassword: "",
        checkPassword: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (value === this.ruleForm.oldPassword) {
                callback(new Error("新密码不能与旧密码相同"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        checkPassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (value !== this.ruleForm.newPassword) {
                callback(new Error("两次输入密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    openModel() {
      this.ruleForm = {
        oldPassword: "",
        newPassword: "",
        checkPassword: "",
      };
      this.$nextTick(() => {
        for (let field in this.$refs) {
          if (this.$refs[field].clearValidate) {
            this.$refs[field].clearValidate();
          }
        }
      });
      this.dialogVisible = true;
    },
    submitForm() {
      // 使用 this.$refs.ruleFormRef.validate 方法触发表单验证
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          const data = {
            oldPassword: this.ruleForm.oldPassword,
            newPassword: this.ruleForm.newPassword,
          };
          updateAdminPassword(data).then((res) => {
            if (res.code === 200) {
              this.$message({
                message: "修改成功,请重新登录！",
                type: "success",
              });
              this.dialogVisible = false;
              // 退出登录
              this.$store.dispatch("user/logout").then(
                () => {
                  this.$router.push(`/login?redirect=${this.$route.fullPath}`);
                }
              );
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>
